package pri.hillchen.std.concurren.chapter7;
import org.apache.log4j.Logger;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by hillchen on 2018/2/10.
 */
public class UncaughtExceptionHandleDemo {
    private static Logger LOGGER = Logger.getLogger(UncaughtExceptionHandleDemo.class);
    public static void main(String[] args){
        AtomicBoolean hasStop = new AtomicBoolean(false);
        ExecutorService executorService = new ThreadPoolExecutor(5, 10, 30, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>());
        for(int i=0;i< 20;i++){
            final int othreadId = i;
            executorService.execute(new Runnable() {
                private int threadId = othreadId;
                @Override
                public void run() {
                    try {
                        int sleepCount = 1;
                        while (!hasStop.get()){
                            if(threadId == 8){
                                Thread.sleep(100);
                                LOGGER.info(String.format("thread-%s %s times sleep !",threadId,sleepCount));
                                if(threadId == 8 && sleepCount ==10){
                                    LOGGER.info(String.format("thread-%s sleep end and throw runtime Exception!",threadId));
                                    hasStop.set(true);
                                    throw  new RuntimeException("exception");
                                }
                                sleepCount++;
                            }
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

    }
}
